package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class WriteDiary {
    public static void write(String user,String diary_title,String body, String datatime,String accept) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = DBUtils.getConnection();

            String sql = "INSERT INTO `" + user + "`(diary_title, diary_body, data_time, accepted) VALUES (?, ?, ?, ?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,diary_title);
            preparedStatement.setString(2,body);
            preparedStatement.setString(3,datatime);
            // 第四位为允许查看的用户，默认为仅允许自己查看
            // 后续添加此功能
            preparedStatement.setString(4,accept);

            int target = preparedStatement.executeUpdate();
            if (target == 1){
                System.out.println("日记新建成功");
            }
            else {
                System.out.println("日记新建失败，请更换较好的网络环境重新尝试或联系管理人员");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
